<?php
namespace app\appclass\controller;
use app\AdminRest;

use app\ApiRest;
use app\appclass\model\CardList;
use app\appclass\model\CardUser;
use app\appclass\model\CardWater;
use app\appclass\model\ClassCard;
use app\appclass\model\ClassCate;
use app\appclass\model\ClassCoach;
use app\appclass\model\ClassDate;
use app\appclass\model\ClassLable;
use app\appclass\model\ClassList;
use app\appclass\model\ClassRecord;
use app\appclass\model\ClassRoom;
use app\appclass\model\Lable;
use app\appclass\model\StoreInfo;
use app\card\model\Company;
use app\shop\model\IndexUser;
use app\shop\model\IndexUserInfo;
use think\App;
use think\facade\Db;


/**
 * @author chenniang
 * @DataTime: 2021-06-04 16:37
 * Class IndexStore
 * @package app\appclass\controller
 *
 * 主要用户门店统计
 */


class IndexStore extends ApiRest
{

    protected $model;

    protected $record_model;

    protected $lable_model;

    protected $card_user_model;

    protected $date_model;

    protected $store_id;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model       = new ClassList();

        $this->record_model= new ClassRecord();

        $this->lable_model = new ClassLable();

        $this->card_user_model = new CardUser();

        $this->date_model  = new ClassDate();

        $this->store_id = $this->model->getStoreId($this->getUserId());

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-03 16:05
     * @功能说明:门店统计
     */
    public function index(){

        $input = $this->_param;

        $dis=[

            'store_id' => $this->store_id
        ];
        //售卡金额
        $data['sale_price'] = $this->card_user_model->getSaleCountOrPrice($dis,[],$input['whereTime'],0);
        //售卡数量
        $data['sale_count'] = $this->card_user_model->getSaleCountOrPrice($dis,[],$input['whereTime'],1);
        //今日预约人数
        $data['today_app']  = $this->record_model->where($dis)->whereTime('create_time','today')->where('status','in',[1,3])->sum('num');
        //会员人数
        $data['card_user_count']  = $this->card_user_model->where(['is_cancel'=>0])->where($dis)->group('user_id')->count();
        //新增访客
        $data['new_user_count']   = $this->card_user_model->todayCustomer($this->store_id);
        //月排课数量
        $data['class_date_count'] = $this->date_model->where($dis)->where('status','<>',-1)->whereTime('start_time','month')->count();
        //成课数量
        $success_count = $this->date_model->where($dis)->where(['status'=>2])->whereTime('start_time','month')->count();
        //月成课率
        $data['month_success_class'] = $data['class_date_count']!=0?round($success_count/$data['class_date_count']*100,2):0;
        //月上座率
        $data['month_success_site'] = $this->model->monthSuccessSite($this->store_id,3);

        $sale_count = $this->card_user_model->where($dis)->whereTime('create_time','month')->count();

        $i = [0,1,2];

        foreach ($i as $value){
            //数量
            $list[$value]['sale_count'] = $this->card_user_model->getSaleCountOrPrice($dis,[],'month',1,$value);
            //类型
            $list[$value]['type']    = $value;
            //比例
            $list[$value]['balance'] = !empty($sale_count)?round($list[$value]['sale_count']/$sale_count*100,2):0;

        }
        //月售卡分析
        $data['month_card'] = $list;

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-03 17:54
     * @功能说明:月售卡明细
     */
    public function monthCardInfo(){

        $input = $this->_param;

        $dis = [

            'store_id' => $this->store_id
        ];

        $i = [0,1,2];

        $sale_count = $this->card_user_model->where($dis)->where('is_cancel','=',0)->whereTime('create_time','month')->count();

        foreach ($i as $value){
            //数量
            $list[$value]['sale_count'] = $this->card_user_model->where($dis)->where('is_cancel','=',0)->where(['card_type'=>$value])->whereTime('create_time','month')->count();

            $list[$value]['sale_price'] = $this->card_user_model->where($dis)->where(['card_type'=>$value])->whereTime('create_time','month')->sum('pay_price');
            //退款金额
            $cancel_price = $this->card_user_model->monthCancelPrice($dis,$value);

            $list[$value]['sale_price'] = round($list[$value]['sale_price']-$cancel_price,2);
            //类型
            $list[$value]['type']    = $value;
            //比例
            $list[$value]['balance'] = !empty($sale_count)?round($list[$value]['sale_count']/$sale_count*100,2):0;

        }

        $data['list'] = $list;

        $data['sale_count'] = $sale_count;

        $data['sale_price'] = $this->card_user_model->where($dis)->whereTime('create_time','month')->sum('pay_price');
        //退款金额
        $cancel_price = $this->card_user_model->monthCancelPrice($dis);

        $data['sale_price'] = round($data['sale_price'] -$cancel_price,2);

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-04 14:30
     * @功能说明:课程月排课分析
     */
    public function monthClassDate(){

        $input = $this->_param;

        $dis[] = ['b.store_id','=',$this->store_id];

        $data = $this->model->monthClassDate($dis);

        if(!empty($data['data'])){

            $cate_model = new ClassCate();

            foreach ($data['data'] as &$v){
                //签到人数
                $v['sign_num'] = $this->record_model->where(['class_id'=>$v['id'],'status'=>3])->whereMonth('start_time')->sum('num');
                //成课数量
                $success_count = $this->date_model->where(['class_id'=>$v['id'],'status'=>2])->whereMonth('start_time')->count();
                //月成课率
                $v['month_success_class'] = $v['date_count']!=0?round($success_count/$v['date_count']*100,2):0;
                //月上桌率
                $v['month_success_site']  = $this->model->monthSuccessSite($v['id'],2);
                //分类名字
                $v['cate_name'] = $cate_model->where(['id'=>$v['cate_id']])->value('title');

            }

        }

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-04 14:30
     * @功能说明:教练月排课分析
     */
    public function coachMonthClassDate(){

        $input = $this->_param;

        $dis[] = ['b.store_id','=',$this->store_id];

        $data = $this->model->coachMonthClassDate($dis);

        if(!empty($data['data'])){

            $cate_model = new ClassCate();

            foreach ($data['data'] as &$v){
                //签到人数
                $v['sign_num'] = $this->record_model->where(['coach_id'=>$v['id'],'status'=>3])->whereMonth('start_time')->sum('num');
                //成课数量
                $success_count = $this->date_model->where(['coach_id'=>$v['id'],'status'=>2])->whereMonth('start_time')->count();
                //月成课率
                $v['month_success_class'] = $v['date_count']!=0?round($success_count/$v['date_count']*100,2):0;
                //月上桌率
                $v['month_success_site']  = $this->model->monthSuccessSite($v['coach_id']);
                //分类名字
                $v['cate_type'] = $cate_model->where(['id'=>$v['cate_id']])->value('type');
                //昵称
                $v['user_name']   = lbUserName($v['id']);
                //头像
                $v['user_avatar'] = lbUserAvatar($v['id']);

            }

        }

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-04 13:35
     * @功能说明:卡金额明细详情(售卡金额，退款金额)
     */
    public function cardPriceInfo(){

        $input = $this->_param;

        $dis=[

            'store_id' => $this->store_id
        ];
        //开卡金额
        $data['sale_price']  = $this->card_user_model->where($dis)->whereTime('create_time',$input['whereTime'])->sum('pay_price');

        $data['sale_price']  = round($data['sale_price'],2);
        //退卡金额
        $data['cancel_price']= $this->card_user_model->cancelPrice(['a.store_id'=>$this->store_id],$input['whereTime']);

        $data['cancel_price'] = round($data['cancel_price'],2);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-04 09:52
     * @功能说明:门店售卡列表
     */
    public function saleCardList(){

        $input = $this->_param;

        $dis = [

            'store_id' => $this->store_id,

            'uniacid'  => $this->_uniacid,

            'is_cancel'=> 0
        ];

        $data = [];
        //昨日 本周 本月查询
        if(!empty($input['whereTime'])){

            $data = $this->card_user_model->where($dis)->whereTime('create_time',$input['whereTime'])->order('create_time desc,id desc')->paginate(10)->toArray();

            $data['pay_price'] = $this->card_user_model->where($dis)->whereTime('create_time',$input['whereTime'])->sum('pay_price');

            $data['pay_price'] = round($data['pay_price'],2);
        }
        //自定义时间查询
        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $where[] = ['create_time','between',"$start_time,$end_time"];

            $data = $this->card_user_model->where($dis)->where($where)->order('create_time desc,id desc')->paginate(10)->toArray();

            $data['pay_price'] = $this->card_user_model->where($dis)->where($where)->sum('pay_price');

            $data['pay_price'] = round($data['pay_price'],2);


        }

        if(!empty($data['data'])){

            $user_model = new IndexUser();

            foreach ($data['data'] as &$v){

                $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);

                if(!empty($v['user_id'])){

                    $v['nickName'] =  $user_model->where(['id'=>$v['user_id']])->value('nickName');

                }else{

                    $v['nickName'] = $v['user_name'];
                }

                $v['adviser_name'] = lbUserName($v['adviser']);

            }

        }

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-07 11:01
     * @功能说明:客户管理
     */
    public function customerData(){

        $dis = [

            'store_id' => $this->store_id,

            'is_cancel'=> 0

        ];
        //所有会员
        $data['member_count'] = $this->card_user_model->where($dis)->group('user_id')->count();
        //本月新增会员
        $data['month_member_count'] = $this->card_user_model->where($dis)->whereTime('create_time','month')->group('user_id')->count();
        //储值不足
        $data['not_enough_value'] = $this->card_user_model->where($dis)->where(['card_type'=>2])->where('true_value','<',200)->count();
        //次数不足
        $data['not_enough_times'] = $this->card_user_model->where($dis)->where(['card_type'=>1])->where('true_value','<',5)->count();
        //即将到期
        $data['expire'] = $this->card_user_model->where($dis)->whereBetween('end_time',[time(),time()+86400*30])->count();
        //30天未上课
        $data['thirty_no_class'] = $this->card_user_model->noClassMember($this->store_id);
        //流失会员
        $data['loss_member'] = $this->card_user_model->where($dis)->where('end_time','<',time()-86400*90)->group('user_id')->count();

        $dis_m[] = ['a.store_id','=',$this->store_id];

        $dis_m[] = ['a.is_cancel','=',0];
        //本月生日
        $data['birthday']    = $this->card_user_model->birthdayUserId($dis_m);

        $data['birthday']    = count($data['birthday']);
        //即将开卡
        $data['will_open']   = $this->card_user_model->where($dis)->whereBetween('open_time',[time(),time()+86400*7])->count();
        //访客
        $data['customer']    = $this->card_user_model->customer($this->store_id);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-11 15:45
     * @功能说明:流失会员
     */
    public function lossMemberList(){

        $input = $this->_param;

        $dis[] = ['a.store_id','=',$this->store_id];

        $dis[] = ['a.end_time','<',time()-86400*90];

        $dis[] = ['a.is_cancel','=',0];

        $where = [];

        if(!empty($input['name'])){

            $where[] = ['b.nickName','like','%'.$input['name'].'%'];

            $where[] = ['c.phone','like','%'.$input['name'].'%'];
        }

        $data = $this->card_user_model->memberList($dis,$where);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-11 15:43
     * @功能说明:访客
     */
    public function customerList(){

        $input = $this->_param;

        $dis = [];

        if(!empty($input['name'])){

            $dis[] = ['c.nickName','like','%'.$input['name'].'%'];
        }
        //访客
        $data = $this->card_user_model->customerList($this->store_id,10,$dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-11 15:39
     * @功能说明:30天未上课的人
     */
    public function thirtyNoClass(){

        $input = $this->_param;

        $data = $this->card_user_model->noClassMember($this->store_id,1);

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-06-08 14:05
     * @功能说明:储值不足200元的会员
     */
    public function notEnoughValue(){

        $input = $this->_param;

        $dis[] = ['a.store_id','=',$this->store_id];

        $dis[] = ['a.card_type','=',2];

        $dis[] = ['a.true_value','<',200];

        $dis[] = ['a.is_cancel','=',0];

        $data = $this->card_user_model->notEnough($dis);

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-08 14:05
     * @功能说明:次数不足5次的会员卡
     */
    public function notEnoughTimes(){

        $input = $this->_param;

        $dis[] = ['a.store_id','=',$this->store_id];

        $dis[] = ['a.card_type','=',1];

        $dis[] = ['a.true_value','<',5];

        $dis[] = ['a.is_cancel','=',0];

        $data = $this->card_user_model->notEnough($dis);

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-08 14:05
     * @功能说明:即将到期的会员卡
     */
    public function expire(){

        $input = $this->_param;

        $dis[] = ['a.store_id','=',$this->store_id];

        $dis[] = ['a.is_cancel','=',0];

        $start_time = time();

        $end_time   = time()+86400*30;
        
        $dis[] = ['a.end_time','between',"$start_time,$end_time"];

        $data = $this->card_user_model->notEnough($dis);

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-11 15:04
     * @功能说明:即将开卡的会员卡
     */
    public function willOpenMemberList(){

        $input = $this->_param;

        $dis[] = ['a.store_id','=',$this->store_id];

        $dis[] = ['a.is_cancel','=',0];

        $start_time = time();

        $end_time   = time()+86400*7;

        $dis[] = ['a.open_time','between',"$start_time,$end_time"];

        $data = $this->card_user_model->notEnough($dis);

        return $this->success($data);


    }

    /**
     * @author chenniang
     * @DataTime: 2021-06-08 10:37
     * @功能说明:会员列表
     */
    public function memberList(){

        $input = $this->_param;

        $dis[] = ['a.store_id','=',$this->store_id];

        $dis[] = ['a.is_cancel','=',0];

        $where = [];

        if(!empty($input['name'])){

            $where[] = ['b.nickName','like','%'.$input['name'].'%'];

            $where[] = ['c.phone','like','%'.$input['name'].'%'];
        }

        $data = $this->card_user_model->memberList($dis,$where);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-08 13:52
     * @功能说明:月新增会员
     */
    public function monthMemberList(){

        $input = $this->_param;

        $dis[] = ['a.store_id','=',$this->store_id];

        $dis[] = ['a.is_cancel','=',0];

        $data = $this->card_user_model->monthMemberList($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-08 13:56
     * @功能说明:本月生日会员
     */

    public function monthBirthdayMemberList(){

        $input = $this->_param;

        $dis[] = ['a.store_id','=',$this->store_id];

        $dis[] = ['a.is_cancel','=',0];


        $data = $this->card_user_model->birthdayUser($dis,10);

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-05-31 15:02
     * @功能说明:用户预约次数排名
     */
    public function userRecordTop(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['b.store_id','=',$this->store_id];

        $dis[] = ['b.status','in',[1,3]];

        $where = [];

        if(!empty($input['name'])){

            $where[] = ['a.nickName','like','%'.$input['name'].'%'];

            $where[] = ['c.phone','like','%'.$input['name'].'%'];
        }

        $data = $this->record_model->userRecordTop($dis,$where);

        return $this->success($data);

    }




}
